import { ApiCall } from "tsrpc";
import { ReqCreateSubWorld, ResCreateSubWorld } from "../../../shared/protocols/worldServer/admin/PtlCreateSubWorld";
import { worldServer } from "../../../WorldServerMain";

export async function ApiCreateSubWorld(call: ApiCall<ReqCreateSubWorld, ResCreateSubWorld>) {
    let subWorld = worldServer.createSubWorld(call.req.subWorldId, call.req.displayId, call.req.subWorldName, call.req.subWorldConfigId, call.req.password);

    if (!subWorld) {
        return call.error('INVALID_CONFIG_ID');
    }

    worldServer.masterServerConn?.sendMsg('admin/UpdateSubWorldState', {
        connNum: worldServer.server.connections.length,
        subWorlds: worldServer.subWorlds.map(v => v.state)
    });

    call.succ({
        subWorldId: subWorld.id
    });
}